package gb.demo.controller;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import gb.demo.common.R;
import gb.demo.entity.*;
import gb.demo.mapper.MovieMapper;
import gb.demo.service.LabelService;
import gb.demo.service.MovieLabelService;
import gb.demo.service.MovieService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.awt.desktop.QuitResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//                    .-o=o-.
//                ,  /=o=o=o=\ .--.
//               _|\|=o=O=o=O=|    \
//           __.'  a`\=o=o=o=(`\   /
//           '.   a 4/`|.-""'`\ \ ;'`)   .---.
//             \   .'  /   .--'  |_.'   / .-._)
//              `)  _.'   /     /`-.__.' /
//           jgs `'-.____;     /'-.___.-'
//                        `"""`

@Slf4j
@Controller
public class MovieController {
    @Autowired
    private MovieService movieService;
    @Autowired
    private MovieMapper movieMapper;
    @Autowired
    private MovieLabelService movieLabelService;
    @Autowired
    private LabelService labelService;

    //    测试
//    @RequestMapping("/movie/MovieTest")
//    public String MovieTest(HttpSession session){
//        QueryWrapper<Movie> wrapper=new QueryWrapper<Movie>();
//        wrapper.select("movieid","moviename","rate","heat").lt("movieid",20).gt("movieid",10);
//        List<Movie> list=movieService.list(wrapper);
//        list.stream().forEach(System.out::println);
//        System.out.println("测试 "+session.getAttribute("loginUser"));
//        return JSON.toJSONString(list);
//    }
    @GetMapping("/Movie/moviePage")
    public String moviePage() {
        return "movie/allMovie";
    }

    @GetMapping("/movie/loveListPage")
    public String loveListPage() {
        return "movie/loveList";
    }

    @GetMapping("/movie/movieManagePage")
    public String movieManagePage() {
        return "movie/movieManagement";
    }

//  全部电影
    @RequestMapping("/movie/allMovie")
    @ResponseBody
    public R allMovie() {
        //获取全部电影
        List<Movie> list = movieService.list();
//      start
        List<movieDetail> movieDetailList = new ArrayList<>();
        for (Movie m : list) {
            QueryWrapper<Movielabel> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("movieid", m.getMovieid());
            List<Movielabel> movielabelList = movieLabelService.list(queryWrapper);
            List<Label> labelList = new ArrayList<>();
            for (Movielabel ml : movielabelList) {
                QueryWrapper<Label> labelQueryWrapper = new QueryWrapper<>();
                labelQueryWrapper.eq("labelid", ml.getLabelid());
                labelList.add(labelService.getOne(labelQueryWrapper));
            }
            movieDetailList.add(new movieDetail(m, labelList));
        }
        return R.success(movieDetailList);
//        end
    }
    //    电影搜索
    @RequestMapping("/movie/search")
    @ResponseBody
    public R search(String keyword) {
        LambdaQueryWrapper<Movie> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        if(keyword==null){
//            keyword=" ";
//        } 关键词不对显示所有电影
        lambdaQueryWrapper.like(Movie::getMoviename, keyword);
        List<Movie> list = movieService.list(lambdaQueryWrapper);
        if (list.isEmpty()) {
            return R.error("不存在该电影");
        }
        List<movieDetail> movieDetailList =new ArrayList<>();
        for(Movie movie:list){
            List<Label> labelList= movieMapper.findMovieLabel(String.valueOf(movie.getMovieid()));
            movieDetailList.add(new movieDetail(movie,labelList));
        }
        return R.success(movieDetailList);
    }

    //    电影详情
    @RequestMapping("/movie/detail/{movieid}")
    @ResponseBody
    public R movieDetail(@PathVariable String movieid) {
        List<Label> list = movieMapper.findMovieLabel(movieid);
        QueryWrapper<Movie> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("movieid", movieid);
        Movie movie = movieService.getOne(queryWrapper);
        List<movieDetail> movieDetailList = new ArrayList<>();
        movieDetailList.add(new movieDetail(movie, list));
        return R.success(movieDetailList);
    }
    //    电影详情
    @RequestMapping("/movie/detail")
    @ResponseBody
    public R movieDetail1(String movieid) {
        List<Label> list = movieMapper.findMovieLabel(movieid);
        QueryWrapper<Movie> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("movieid", movieid);
        Movie movie = movieService.getOne(queryWrapper);
        List<movieDetail> movieDetailList = new ArrayList<>();
        movieDetailList.add(new movieDetail(movie, list));
        return R.success(movieDetailList);
    }

    //  推荐电影
    @RequestMapping("/movie/recommend")
    @ResponseBody
    public R MoiveRecommend(HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        System.out.println(user);
        if (user != null) {
            movieMapper.set();
            List<Movie> list = movieMapper.recommend(user.getUserid());
            if (list == null) {
                return R.error("暂时没有推荐电影哦");
            }
            List<movieDetail> movieDetailList = new ArrayList<>();
            for (Movie m : list) {
                QueryWrapper<Movielabel> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("movieid", m.getMovieid());
                List<Movielabel> movielabelList = movieLabelService.list(queryWrapper);
                List<Label> labelList = new ArrayList<>();
                for (Movielabel ml : movielabelList) {
                    QueryWrapper<Label> labelQueryWrapper = new QueryWrapper<>();
                    labelQueryWrapper.eq("labelid", ml.getLabelid());
                    labelList.add(labelService.getOne(labelQueryWrapper));
                }
                movieDetailList.add(new movieDetail(m, labelList));
            }
            return R.success(movieDetailList);
        } else return R.error("请登陆后重试");
    }

    //电影信息修改
    @RequestMapping("/movie/detail/update")
    @ResponseBody
    public R movieUpdate(String movieid,float heat, HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        if (user != null) {
            UpdateWrapper<Movie> movieUpdateWrapper=new UpdateWrapper<>();
            movieUpdateWrapper.eq("movieid",movieid).set("heat",heat);
            movieService.update(null,movieUpdateWrapper);
            return R.success("修改成功");

        } else return R.error("失败");
    }
    //电影删除
    @RequestMapping("/movie/detail/delete")
    @ResponseBody
    public R movieuDelete(String movieid, HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        if (user != null && user.getIsadmin() == 1) {
            if (movieService.removeById(movieid))
                return R.success("删除成功");
            else return R.error("删除失败");
        } else return R.error("请登录管理员账号重试");
    }
//    电影插入
    @RequestMapping("/movie/detail/insert")
    @ResponseBody
    public R movieInsert(Movie movie, HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        if (user != null && user.getIsadmin() == 1) {
            QueryWrapper<Movie> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("moviename", movie.getMoviename());
            Movie t = movieService.getOne(queryWrapper);
            if (t != null)
                return R.error("该电影已经存在");
            if (movieService.save(movie)) {
                return R.success("插入成功");
            } else return R.error("插入失败");
        } else return R.error("请登录管理员账号重试");
    }
    // 电影分类查看
    @RequestMapping("/movie/movieType")
    @ResponseBody
    public R movieType(String labelid){
        QueryWrapper<Movielabel> movielabelQueryWrapper=new QueryWrapper<>();
        movielabelQueryWrapper.eq("labelid",labelid);
        List<Movielabel> movielabelList=movieLabelService.list(movielabelQueryWrapper);
        if(!movielabelList.isEmpty()){
            List<movieDetail> movieDetailList=new ArrayList<>();
            for(Movielabel i:movielabelList){
                QueryWrapper<Movie> movieQueryWrapper=new QueryWrapper<>();
                movieQueryWrapper.eq("movieid",i.getMovieid());
                Movie movie=movieService.getOne(movieQueryWrapper);
                //根据电影查标签
                QueryWrapper<Movielabel> movielabelQueryWrapper1=new QueryWrapper<>();
                movielabelQueryWrapper1.eq("movieid",movie.getMovieid());
                List<Movielabel> allLabel=movieLabelService.list(movielabelQueryWrapper1);
                List<Label> labelList=new ArrayList<>();
                for(Movielabel ml:allLabel){
                    QueryWrapper<Label> labelQueryWrapper = new QueryWrapper<>();
                    labelQueryWrapper.eq("labelid", ml.getLabelid());
                    labelList.add(labelService.getOne(labelQueryWrapper));
                }
                movieDetailList.add(new movieDetail(movie,labelList));
            }
            return R.success(movieDetailList);
        }
        else return R.error("看你嘛呢");
    }
    // 热门电影
    @RequestMapping("/movie/trendMovie")
    @ResponseBody
    public R  trendMovie(){
        List<Movie> movieList= movieMapper.getTop20();
        List<movieDetail> movieDetailList=new ArrayList<>();
        for(Movie movie:movieList){
            String movieid= String.valueOf(movie.getMovieid());
            List<Label> list = movieMapper.findMovieLabel(movieid);
            movieDetailList.add(new movieDetail(movie,list));
        }
        return R.success(movieDetailList);
    }
    //电影按照年份查找
    @RequestMapping("/movie/CategorizeMoviesByYear")
    @ResponseBody
    public R year(String updateyear){
        List<Movie> movieList=movieMapper.movieGoroup(updateyear);
        List<movieDetail> movieDetailList=new ArrayList<>();
        if(!movieList.isEmpty()){
            for (Movie movie:movieList){
                List<Label> labelList=movieMapper.findMovieLabel(String.valueOf(movie.getMovieid()));
                movieDetailList.add(new movieDetail(movie,labelList));
            }
            return R.success(movieDetailList);
        }
        else return R.error("没有相关电影哦");
    }
    //电影按照 年份、分类 查找
    @RequestMapping("/movie/CategorizeMoviesByYearAndLabel")
    @ResponseBody
    public R YearLabel(String updateyear,String labelid){
        List<Movie> movieList=movieMapper.movieGroupLabelAndYear(updateyear,labelid);
        if(movieList.isEmpty()){
            return R.error("没有相关电影哦");
        }
        List<movieDetail> movieDetailList=new ArrayList<>();
        for(Movie movie:movieList){
            List<Label> labelList=movieMapper.findMovieLabel(String.valueOf(movie.getMovieid()));
            movieDetailList.add(new movieDetail(movie,labelList));
        }
        return R.success(movieDetailList);
    }
}


